#include "HotKey.h"
#include "MsgWinApiLastError.h"
#include <iostream>


namespace TLib
{

	HotKey::HotKey(HWND hWnd, 
		unsigned int vk, 
		int id, 
		bool ctrl, 
		bool alt, 
		bool shift)
	{
		this->hWnd  = hWnd;					  
		this->id    = id;
		this->ctrl  = ctrl;
		this->alt   = alt;
		this->shift = shift;
		this->vk    = vk;

		recalculateHotkeyStr();
		Register();
	}

	HotKey::~HotKey()
	{
		UnRegister();
	}
	
	void HotKey::recalculateHotkeyStr()
	{
		hotkeyStr = "";
		if (ctrl)
			hotkeyStr += "CTRL + ";
		if (alt)
			hotkeyStr += "ALT + ";
		if (shift)
			hotkeyStr += "SHIFT + ";
		
		hotkeyStr += (char)LOWORD(MapVirtualKey(vk, MAPVK_VK_TO_CHAR));
		
	}

	void HotKey::Register()
	{
		unsigned int modifiers = 0;
		if (shift)
			modifiers |= MOD_SHIFT;
		if (ctrl)
			modifiers |= MOD_CONTROL;
		if (alt)
			modifiers |= MOD_ALT;
		registered = true;
		
		std::cout << "---Registered hotkey---\nshift=" << 
		shift << "\nctrl=" << ctrl << "\nalt=" << alt << 
		"\nid=" << id << "\nvk=" << vk << '\n';
		if (!RegisterHotKey
			(
			hWnd,
			id,
			modifiers,
			vk
			))
		{
			MsgWinApiLastError("RegisterHotKey");
		}
	}
	void HotKey::UnRegister()
	{
		if (!UnregisterHotKey(hWnd, id))
			MsgWinApiLastError("UnregisterHotKey");
		registered = false;
	}

}// Namespace TLib